﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using System.Text.RegularExpressions;
using FBEntities.FBConnections.User;

namespace FBEntities.FBObjects.User
{
    public class FBPerson : IFBUpdatable
    {
        //public event EventHandler FinishParse;
        public bool Parsed;

        public string id;
        public string name;
        public string first_name;
        public string last_name;
        public string link;
        public string about;
        public DateTime? birthday;
        public List<FBWork> work;
        public List<FBEducation> education;
        public FBPerson significant_other;
        public string email;
        public FBObject hometown;
        public FBObject location;
        public string gender;
        public string[] interested_in;
        public string[] meeting_for;
        public string relationship_status;
        public string religion;
        public string political;
        public string website;
        public string bio;
        public string quotes;
        public int timezone;
        public bool verified;
        public DateTime updated_time;

        public FBHome Home
        {
            get
            {
                if (home == null)
                    home = new FBHome();

                if (!home.Parsed)
                    home.ParseCached(id);

                return home;
            }
            protected set
            {
                home = value;
            }
        }
        public FBFeed Feed
        {
            get
            {
                if (feed == null)
                    feed = new FBFeed();

                if (!feed.Parsed)
                    feed.ParseCached(id);

                return feed;
            }
            protected set
            {
                feed = value;
            }
        }
        public FBTagged Tagged
        {
            get
            {
                if (tagged == null)
                    tagged = new FBTagged();

                if (!tagged.Parsed)
                    tagged.ParseCached(id);

                return tagged;
            }
            protected set
            {
                tagged = value;
            }
        }
        public FBPosts Posts
        {
            get
            {
                if (posts == null)
                    posts = new FBPosts();

                if (!posts.Parsed)
                    posts.ParseCached(id);

                return posts;
            }
            protected set
            {
                posts = value;
            }
        }
        public FBPicture Picture
        {
            get
            {
                if (picture == null)
                    picture = new FBPicture(id);

                return picture;
            }
            protected set
            {
                picture = value;
            }
        }
        public FBFriends Friends
        {
            get
            {
                if (friends == null)
                    friends = new FBFriends();

                if (!friends.Parsed)
                    friends.ParseCached(id);

                return friends;
            }
            protected set
            {
                friends = value;
            }
        }
        public FBActivities Activities
        {
            get
            {
                if (activities == null)
                    activities = new FBActivities();

                if (!activities.Parsed)
                    activities.ParseCached(id);

                return activities;
            }
            protected set
            {
                activities = value;
            }
        }
        public FBInterests Interests
        {
            get
            {
                if (interests == null)
                    interests = new FBInterests();

                if (!interests.Parsed)
                    interests.ParseCached(id);

                return interests;
            }
            protected set
            {
                interests = value;
            }
        }
        public FBMusic Music
        {
            get
            {
                if (music == null)
                    music = new FBMusic();

                if (!music.Parsed)
                    music.ParseCached(id);

                return music;
            }
            protected set
            {
                music = value;
            }
        }
        public FBBooks Books
        {
            get
            {
                if (books == null)
                    books = new FBBooks();

                if (!books.Parsed)
                    books.ParseCached(id);

                return books;
            }
            protected set
            {
                books = value;
            }
        }
        public FBMovies Movies
        {
            get
            {
                if (movies == null)
                    movies = new FBMovies();

                if (!movies.Parsed)
                    movies.ParseCached(id);

                return movies;
            }
            protected set
            {
                movies = value;
            }
        }
        public FBTelevision Television
        {
            get
            {
                if (television == null)
                    television = new FBTelevision();

                if (!television.Parsed)
                    television.ParseCached(id);

                return television;
            }
            protected set
            {
                television = value;
            }
        }
        public FBLikes Likes
        {
            get
            {
                if (likes == null)
                    likes = new FBLikes();

                if (!likes.Parsed)
                    likes.ParseCached(id);

                return likes;
            }
            protected set
            {
                likes = value;
            }
        }
        public FBPhotos Photos
        {
            get
            {
                if (photos == null)
                    photos = new FBPhotos();

                if (!photos.Parsed)
                    photos.ParseCached(id);

                return photos;
            }
            protected set
            {
                photos = value;
            }
        }
        public FBAlbums Albums
        {
            get
            {
                if (albums == null)
                    albums = new FBAlbums();

                if (!albums.Parsed)
                    albums.ParseCached(id);

                return albums;
            }
            protected set
            {
                albums = value;
            }
        }
        public FBStatuses Statuses
        {
            get
            {
                if (statuses == null)
                    statuses = new FBStatuses();

                if (!statuses.Parsed)
                    statuses.ParseCached(id);

                return statuses;
            }
            protected set
            {
                statuses = value;
            }
        }
        public FBLinks Links
        {
            get
            {
                if (links == null)
                    links = new FBLinks();

                if (!links.Parsed)
                    links.ParseCached(id);

                return links;
            }
            protected set
            {
                links = value;
            }
        }
        public FBNotes Notes
        {
            get
            {
                if (notes == null)
                    notes = new FBNotes();

                if (!notes.Parsed)
                    notes.ParseCached(id);

                return notes;
            }
            protected set
            {
                notes = value;
            }
        }
        public FBEvents Events
        {
            get
            {
                if (events == null)
                    events = new FBEvents();

                if (!events.Parsed)
                    events.ParseCached(id);

                return events;
            }
            protected set
            {
                events = value;
            }
        }
        public FBInbox Inbox
        {
            get
            {
                if (inbox == null)
                    inbox = new FBInbox();

                if (!inbox.Parsed)
                    inbox.ParseCached(id);

                return inbox;
            }
            protected set
            {
                inbox = value;
            }
        }
        public FBInbox Outbox
        {
            get
            {
                if (outbox == null)
                    outbox = new FBInbox();

                if (!outbox.Parsed)
                    outbox.ParseCached(id);

                return outbox;
            }
            protected set
            {
                outbox = value;
            }
        }

        protected FBHome home;
        protected FBFeed feed;
        protected FBTagged tagged;
        protected FBPosts posts;
        protected FBPicture picture;
        protected FBFriends friends;
        protected FBActivities activities;
        protected FBInterests interests;
        protected FBMusic music;
        protected FBBooks books;
        protected FBMovies movies;
        protected FBTelevision television;
        protected FBLikes likes;
        protected FBPhotos photos;
        protected FBAlbums albums;
        protected FBStatuses statuses;
        protected FBLinks links;
        protected FBNotes notes;
        protected FBEvents events;
        protected FBInbox inbox;
        public FBInbox outbox;
        //public FBUpdates updates;

        public FBPerson()
        {
            Parsed = false;
            work = new List<FBWork>();
            education = new List<FBEducation>();
        }

        #region IFBObject Members

        public void Parse(JObject o)
        {
            Parsed = true;
            FBParseUtilities.Parse(o, this);

            if (Updated != null)
                Updated(this, new EventArgs());
        }

        public void Parse()
        {
            if (string.IsNullOrEmpty(id))
                return;

            string url = "https://graph.facebook.com/" + id + "?" + FBParseUtilities.authKey;
            string json = FBCache.GetJSON("person-" + id, url);

            if (!string.IsNullOrEmpty(json))
            {
                JObject o = JObject.Parse(json);
                Parse(o);
            }
        }

        public bool UpdateStatus(string status)
        {
            FBParseUtilities.PostTextREST(id, "status.set", new string[] { "status", status });

            FBCache.BlockNextCache = true;
            statuses.data.Clear();
            statuses.Parse(id);

            if (Updated != null)
                Updated(this, new EventArgs());

            return true;
        }

        public void ParseAsync(JObject text)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IFBUpdatable Members

        public void Update()
        {
            FBCache.BlockNextCache = true;
            Parse();
            if (FBCache.BlockNextCache)
                FBCache.BlockNextCache = false;
        }

        public event UpdateEventHandler Updated;

        #endregion
    }
}
